package pers.xiaoliang.concurrency.example.singleton;

import lombok.extern.slf4j.Slf4j;
import pers.xiaoliang.concurrency.annotation.Recommend;
import pers.xiaoliang.concurrency.annotation.ThreadSafe;

/**
* @description: 单例发布对象 - 使用枚举 （不会造成资源浪费）
* @author: xiaoliang.liu
* @date: 2018/7/28 13:36
* @version v1.0
*/
@Slf4j
@ThreadSafe
@Recommend
public class SingletonExample7 {

    private SingletonExample7() {}

    /** 从枚举中获得 */
    public static SingletonExample7 getInstance() {
        return Singleton.INSTANCE.getInstance();
    }

    /** 内部枚举类 */
    private enum Singleton {
        /** SingletonExample7 实例 */
        INSTANCE;

        private SingletonExample7 singleton;

        /** 从JVM级别保证这个构造方法只会调用一次 */
        Singleton() {
            singleton = new SingletonExample7();
        }

        public SingletonExample7 getInstance() {
            return singleton;
        }
    }




    public static void main(String[] args) {
        System.out.println(getInstance());
    }
}
